Repository navigation
perf: packed histograms skip empty subtrees when scanning counts - #69
Merged
Merged
Conversation
alexvictoor
force-pushed
the
perf/packed-nonzero-iteration
branch
from
November 8, 2026 17:38
e513b05 to
ecf8b9b
Compare
Scanning a PackedHistogram's counts (percentile lookup, add, subtract, re-establishing min/max/total, encoding) used to visit every virtual index and walk the set trees for each group of 8, so the cost grew with the counts array length rather than with the values actually recorded. PackedArray now exposes nextNonZeroIndex(fromIndex): on a packed context it walks only the populated slots of the set-0 tree (sets are populated in order 0..7, so any group holding a non-zero value has a set-0 leaf) and reads the 8 values of each candidate group; on an unpacked context it scans the longs linearly. Both the AssemblyScript and the JS histograms use it in their packed-specific scan paths, through a nextNonZeroIndex hook on the histogram (default: linear scan, TypedArrayHistogram reads its counts directly). Outputs are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
force-pushed
the
perf/packed-nonzero-iteration
branch
from
November 14, 2026 20:28
ecf8b9b to
c94b072
Compare
…ions Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
marked this pull request as ready for review
November 15, 2026 19:12
This was referenced Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Scanning a
PackedHistogramvisited every virtual index, so its cost was proportional to the virtual length even when only a few hundred counts were populated. Packed scans now jump from one populated leaf group to the next.The primitive, in both implementations (
assembly/packedarray,src/packedarray):flowchart TD S["PackedArray.nextNonZeroIndex(from)"] --> U{"context unpacked? (storage full, #66)"} U -- yes --> L["linear scan of the long array"] U -- no --> T["PackedArrayContext.nextIndexWithLeaf(from): descend the set-0 tree"] T --> P["at each entry visit only the slots flagged in its bitmask (slot = popcount of the lower bits)"] P --> G["first leaf group at or after from"] G --> R{"any of its 8 counts non-zero? (read through the leaf cache)"} R -- yes --> I["return that index"] R -- no --> N["continue from the next group"] --> TWhy set 0 is enough: sets are populated in order 0..7, so any group with a leaf in any set has a set-0 leaf. The set-0 leaf can still be all zero (a count of 256 has byte 0 = 0 under byte 1 = 1), hence the per-group check.
Consumers:
Histogram:nextNonZeroIndexdispatched at compile time viaidof<T>(); the jump is inserted in the scalar loops, guarded by!isFlatStorage<T>(), so the SIMD kernels of flat histograms are untouched.encoding.tsmeasures zero runs with it.JsHistogram.nextNonZeroIndex(@internal, linear by default), overridden byPackedHistogram(tree walk) andTypedArrayHistogram(direct_countsread).PackedHistogramalso overridesgetValueAtPercentile. Rebased on perf: bound counts scans by min and max recorded values #70:add/subtractloops use a newnextNonZeroIndexInRange(from, last)that combines the min/max bounds with the jump.No public API change.
Evidence
src/bench/histogram-sparse-packed.bench.ts(new: 300 values over the safe-integer range, 3 significant digits) run on master2b0e3ed.After: same bench on this branch. ops/s, higher is better.
*
PackedHistogramnow overridesgetValueAtPercentile, so the baseJsHistogrammethod'sgetCountAtIndexcall site stays monomorphic for typed-array histograms in a process that also uses packed ones.After:
assembly/__tests__/PackedArrayIteration.test.tsand fast-check properties insrc/packedarray/PackedArray.fc.spec.tscompare the walk against a brute-force scan (sparse, dense, byte-0-zero values, set back to 0, resize, unpacked fallback);src/PackedHistogram.spec.tscompares a sparse packed histogram with Int32 / Float64 twins (percentiles, encoded bytes incl. after subtract, add / subtract across layouts) and WASM packed with JS packed.npm run astest:ci: 15377/15377.npm test: 284 passed.Merge Danger
Door: two-way
Internal hooks only; a plain
git revertrestores the linear scans. The encoded bytes are unchanged (the encoder only uses the hook to measure zero runs, and the tests compare them against the flat histograms).Blast Radius: scans
Every packed-histogram scan (percentiles, add, subtract, encode, re-establishing min/max/total after subtract) and, on the JS side, the
nextNonZeroIndexhook used byadd/subtractfor typed-array histograms. A wrong jump would silently skip counts, which is what the brute-force and twin comparisons guard against.🤖 Generated with Claude Code